package top.easyblog.web.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import top.easyblog.common.bean.UrlMappingBean;
import top.easyblog.common.request.CreateUrlMappingRequest;
import top.easyblog.core.service.UrlMappingService;
import top.easyblog.web.annotation.ResponseWrapper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * @author: frank.huang
 * @date: 2023-12-17 23:40
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class UrlMappingController {


    @Autowired
    private final UrlMappingService urlMappingService;


    @ResponseWrapper
    @PostMapping("/link")
    public UrlMappingBean createUrlMapping(@Valid @RequestBody CreateUrlMappingRequest request) {
        return urlMappingService.createUrlMapping(request);
    }


    @GetMapping("/{code}")
    @ResponseStatus(HttpStatus.FOUND)
    public void dispatch(@PathVariable(name = "code") String code, HttpServletRequest request, HttpServletResponse response) {
        urlMappingService.dispatch(code, request, response);
    }

}
